package com.atguigu.dga230301.governance.assess.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga230301.governance.assess.Assessor;
import com.atguigu.dga230301.governance.bean.AssessParam;
import com.atguigu.dga230301.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("NO_ACCESS")
public class NoAccessAssess extends Assessor {
    @Override
    protected void assessor(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        //一张表{days}天内没有访问 则给0分 ， 其余给10

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //1.获取考评表的最后访问时间
        Date tableLastAccessTimeDate = assessParam.getTableMetaInfo().getTableLastAccessTime();
//        tableLastAccessTimeDate.getTime()/1000/60/60/24
        String lastAccessTimeString = sdf.format(tableLastAccessTimeDate);
        Date lastAccess = sdf.parse(lastAccessTimeString);
        long lastAccessTimestamp = lastAccess.getTime();


        //2.获取考评表的考评时间
        String assessDateString = assessParam.getAssessDate();
        Date assessDate = sdf.parse(assessDateString);
        long assessDateTimestamp = assessDate.getTime();

        //3.获取到阈值
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject metricParamJsonObj = JSON.parseObject(metricParamsJson);
        Long days = metricParamJsonObj.getLong("days");

        //4.将两个时间相减和阈值做判断看是否大于阈值如果大于的话给0分
        long diffdays = (assessDateTimestamp - lastAccessTimestamp) / 1000 / 60 / 60 / 24;

        if (diffdays>days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期无访问");
            governanceAssessDetail.setAssessComment("距上一次访问："+diffdays+"天");
        }
    }
}
